<?php

namespace App\Controllers;

use App\Controllers\BaseController;
use Illuminate\Http\Request;
use App\Services\AdminService;
use common\enum\CommonEnum;
use Illuminate\Support\Facades\Log;

/**
 * 管理员控制类
 */
class AdminController extends BaseController
{
    public function getAdminList(Request $request)
    {
        $params = $this->validate($request, [
            'username' => 'string|max:30',
        ]);
        $page = $request->input('page', 1);
        $pageSize = $request->input('page_size', 20);
        $res = AdminService::getAdminList($params,$page,$pageSize);
        return $res;
    }
    public function addAdmin(Request $request)
    {
        $params = $this->validate($request,[
            'username'=>[
                'required',
                'regex:/^[a-zA-Z0-9_-]{4,16}$/'
            ],
            'password'=>[
                'required',
                'regex:/^[a-zA-Z0-9_-]{6,16}$/',
                'confirmed'
            ],
            'password_confirmation' => [
                'required',
                'regex:/^[a-zA-Z0-9_-]{6,16}$/',
                'same:password'
            ],
            'role'=> 'required|integer',
            'expire_time'=>'required|date_format:Y-m-d H:i:s',
            'white_list' =>'string|max:200',
            'truename'=>'string|max:60',
            'part'=>'string|max:20',
            'job'=>'string|max:255'
        ]);
        return AdminService::addAdmin($params);
    }
    public function editAdmin(Request $request)
    {
        $this->validate($request,[
            'type'=>'required',
            'admin_id'=>'required|integer'
        ]);
        $type = $request->input('type');
        $id = $request->input('admin_id');
        switch ($type)
        {
            case 'status':
                $params = $this->validate($request,[
                    'status' => 'required|Boolean',
                ]);
                break;
            case 'info':
                $params = $this->validate($request,[
                    'username'=>[
                        'required',
                        'regex:/^[a-zA-Z0-9_-]{4,16}$/'
                    ],
                    'role'=> 'required|integer',
                    'expire_time'=>'required|date_format:Y-m-d H:i:s',
                    'white_list' =>'string|max:200',
                    'password'=>[
                        'regex:/^[a-zA-Z0-9_-]{6,16}$/',
                        'confirmed'
                    ],
                    'password_confirmation' => [
                        'regex:/^[a-zA-Z0-9_-]{6,16}$/',
                        'same:password'
                    ],
                    'truename'=>'string|max:60',
                    'part'=>'string|max:20',
                    'job'=>'string|max:255'
                ]);
                break;
            case 'password':
                $params = $this->validate($request,[
                    'password'=>[
                        'regex:/^[a-zA-Z0-9_-]{6,16}$/',
                        'confirmed'
                    ],
                    'password_confirmation' => [
                        'regex:/^[a-zA-Z0-9_-]{6,16}$/',
                        'same:password'
                    ],
                ]);
                break;
            default:
                return [];
        }
        return AdminService::editAdmin($id,$params,$type);
    }
    public function delAdmin(Request $request)
    {
        $this->validate($request,[
            'admin_id'=>'required|integer',
        ]);
        $id = $request->input('admin_id');
        return AdminService::delAdmin($id);
    }

    //踢线管理员
    public function kickAdminOut(Request $request)
    {
        $params = $this->validate($request,[
            'admin_id'=>'required|integer'
        ]);
        return AdminService::kickAdminOut($params['admin_id']);
    }
}
